package com.itestor.iptv.utils;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.hibernate.Session;

/**
 * 
 * @author MR.li.
 * 
 */
public class JdbcUtil {

    /**
     * logger.
     */
    private static final Logger logger = Logger.getLogger(JdbcUtil.class);

    /**
     * 释放资源
     * @param ps
     * @param conn
     * @param hibernateSession
     */
    public static void close(PreparedStatement ps, Connection conn,
            Session hibernateSession) {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                logger.error("closeConn() error : " + e.getMessage());
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                logger.error("closeConn() error : " + e.getMessage());
                e.printStackTrace();
            }
        }
        if (hibernateSession != null) {
            try {
                hibernateSession.close();
            } catch (Exception e) {
                logger.error("closeSess() error : " + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    /**
     * 释放资源.
     * @param rs
     * @param ps
     * @param conn     
     */
    public static void close(ResultSet rs, PreparedStatement ps, Connection conn) {

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                logger.error("closeConn() error : " + e.getMessage());
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                logger.error("closeConn() error : " + e.getMessage());
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                logger.error("closeConn() error : " + e.getMessage());
                e.printStackTrace();
            }
        }

    }
    /**
     * 释放资源
     * @param rs
     * @param callStmt
     * @param conn
     */
    public static void close(ResultSet rs, CallableStatement callStmt, Connection conn){
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                logger.error("closeConn() error : " + e.getMessage());
                e.printStackTrace();
            }
        }
        if (callStmt != null) {
            try {
                callStmt.close();
            } catch (SQLException e) {
                logger.error("closeConn() error : " + e.getMessage());
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                logger.error("closeConn() error : " + e.getMessage());
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 释放资源
     * @param callStmt
     * @param conn
     */
    public static void close(CallableStatement callStmt, Connection conn){
      
        if (callStmt != null) {
            try {
                callStmt.close();
            } catch (SQLException e) {
                logger.error("closeConn() error : " + e.getMessage());
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                logger.error("closeConn() error : " + e.getMessage());
                e.printStackTrace();
            }
        }
    }

}
